jQuery(document).ready(function(){
	totalDeliveryFee();
	init();
});

function ajaxDelivery(d,m,s){
	var weight= $("#cart_weight").html();
	$.post(URLPrefix.url+"/cart/ajaxtotalDelivery.jspx", {
		'deliveryMethod':d,
		'storeId':m,
		'itemStr':s
	}, function(data) {
		if(data.status==1){
			var freight=data.freight;
			if(m==''){
				$("#weight_price_0").html(freight);
				totalDeliveryFee();
			}else{
				$("#weight_price_"+m).html(freight);
				totalDeliveryFee();
			}
		}
	},'json');
	
}

function totalDeliveryFee(){
	var weightprice=0.0;
	$("#[id^='weight_price_']").each(function(){
		weightprice+=parseFloat(jQuery(this).html());
		$("#totalDeliveryFee").html(weightprice);
	});
	init();
}


function init(){
	var price=0.0;
	var weight=0.0;
	var price=0.0;
	var spid;
		$("#[id^='cart_weight_']").each(function(){
		var w=parseFloat(jQuery(this).html());	
		weight =accAdd(weight,w);	
		$("#cart_weight").html(weight);
		});
		$("#[id^='cart_price_']").each(function(){
			price+=parseFloat(jQuery(this).html());
			$("#cart_price").html(price);
		});
		var cart_price= $("#cart_price").html();
		var popularityPrice=$("#popularityPrice").html(); 
		var weightprice=$("#totalDeliveryFee").html(); 
		$("#amount_payable").html(parseFloat(cart_price)+parseFloat(weightprice)-parseFloat(popularityPrice));
}


function accAdd(arg1,arg2){ 
	var r1,r2,m; 
	try{r1=arg1.toString().split(".")[1].length;}catch(e){r1=0;} 
	try{r2=arg2.toString().split(".")[1].length;}catch(e){r2=0;} 
	m=Math.pow(10,Math.max(r1,r2)); 
	return (arg1*m+arg2*m)/m; 
} 

function accAdd(arg1,arg2){ 
	var r1,r2,m; 
	try{r1=arg1.toString().split(".")[1].length;}catch(e){r1=0;} 
	try{r2=arg2.toString().split(".")[1].length;}catch(e){r2=0;} 
	m=Math.pow(10,Math.max(r1,r2)); 
	return (arg1*m+arg2*m)/m; 
} 



//获得城市
function getResultCity(d){
	$.post(URLPrefix.url+"/shopMemberAddress/findAllCity.jspx", {"id":d}, 
		function(data) {
					if(data.success){
						$("#itemcity").html("");
                         var select="<select name='cityId' onchange='getResultCountry(this.value)'>";
                        for(var i=0;i<data.ids.length;i++){
              				select+="<option value='"+data.ids[i]+"'>&nbsp;"+data.citys[i]+"&nbsp;</option>";
              			}
              			select+="</select>";
              			getResultCountry(data.ids[0]);
              			$("#itemcity").append(select);
					} 
	 },"json");
}
//获得县、区
function getResultCountry(a){
	$.post(URLPrefix.url+"/shopMemberAddress/findAllCountry.jspx", {"id":a},
		 function(data) {
				if(data.success){
					$("#itemarea").html("");
                     var select="<select name='countryId'>";
                      for(var i=0;i<data.ids.length;i++){
              				select+="<option value='"+data.ids[i]+"'>&nbsp;"+data.areas[i]+"&nbsp;</option>";
              		 }
              		select+="</select>";
              		$("#itemarea").append(select);
				} 
		},"json");
}

